package com.caishi.lkx.exam.question;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.caishi.lkx.exam.model.QuestionModel;
import com.zzw.common.utils.MD5Util;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 *
 */
public abstract class AbstractQuestionDataProcess {

    /**
     * 判断试题显示数据是否为标准格式
     *
     * @param data
     * @return
     */
    public static boolean questionDataIsStandard(List<Map<String, Object>> data) {
        if (data.size() == 1 && "html".equals(data.get(0).get("tp"))) {
            return Boolean.TRUE.equals(data.get(0).get("standard"));
        }
        return true;
    }

    /**
     * 在试题显示数据中是否有内容
     *
     * @param data
     * @return
     */
    public static boolean questionDataIsNotEmpty(List<Map<String, Object>> data) {
        return CollUtil.isNotEmpty(data) && data.stream().anyMatch(e -> ObjectUtil.isNotEmpty(e.get("vl")));
    }

    /**
     * 试题显示结构中数据是否合法
     *
     * @param data
     * @return
     */
    public static boolean questionShowDataCheck(List<Map<String, Object>> data) {
        return data.stream().allMatch(e -> e.containsKey("tp") && e.containsKey("vl"));
    }


    public double computedQualityLevel(QuestionModel questionModel) {
        return computedStemQualityLevel(questionModel) + computedAnswerQualityLevel(questionModel) + computedDescQualityLevel(questionModel) + computedRightAnswerQualityLevel(questionModel);
    }

    /**
     * 试题内容签名
     *
     * @param questionModel
     * @return
     */
    public String questionContentSign(QuestionModel questionModel) {
        String stem = JSON.toJSONString(questionModel.getStem());
        String answer =  JSON.toJSONString(questionModel.getAnswer());
        String answerDesc = JSON.toJSONString(questionModel.getAnswerDesc());
        return MD5Util.getMessageDigest((stem + answer + answerDesc).getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 试题内容校验
     *
     * @param questionModel
     * @return
     */
    public boolean questionContentCheck(QuestionModel questionModel) {
        return questionDataIsNotEmpty(questionModel.getStem()) && questionShowDataCheck(questionModel.getStem());
    }


    /**
     * 题干质量计算
     *
     * @param questionModel
     * @return
     */
    protected double computedStemQualityLevel(QuestionModel questionModel) {
        boolean stemStandard = questionDataIsStandard(questionModel.getStem());
        return stemStandard ? 10 : 3;
    }

    /**
     * 解析质量计算
     *
     * @param questionModel
     * @return
     */
    protected double computedDescQualityLevel(QuestionModel questionModel) {
        //解析(10)：结构化并且视频解析10分，文字结构化9分，富文本结构化8分，图片结构化7分，其他结构化6分，非结构化4分，不存在解析0分
        boolean answerDescStandard = questionDataIsStandard(questionModel.getAnswerDesc());
        if (answerDescStandard) {
            if (questionModel.getAnswerDesc().stream().anyMatch(e -> "video".equals(e.get("tp")))) {
                return 10;
            } else if (questionModel.getAnswerDesc().stream().anyMatch(e -> "text".equals(e.get("tp")))) {
                return 9;
            } else if (questionModel.getAnswerDesc().stream().anyMatch(e -> "html".equals(e.get("tp")))) {
                return 8;
            } else if (questionModel.getAnswerDesc().stream().anyMatch(e -> "img".equals(e.get("tp")))) {
                return 7;
            } else {
                return 6;
            }
        } else if (questionDataIsNotEmpty(questionModel.getAnswerDesc())) {
            return 4;
        }
        return 0;
    }

    /**
     * 选项质量计算
     *
     * @param questionModel
     * @return
     */
    protected abstract double computedAnswerQualityLevel(QuestionModel questionModel);

    /**
     * 正确答案质量计算
     *
     * @param questionModel
     * @return
     */
    protected abstract double computedRightAnswerQualityLevel(QuestionModel questionModel);

}
